package com.hswealth.demo;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyImage extends JPanel{
	
	BufferedImage bgImage;
	
	public static void main(String[] args) {
		
		//创建窗口
		JFrame frame = new JFrame();
		//设置大小
		frame.setSize(1000, 700);
		//设置标题
		frame.setTitle("Java版电子相册");
		//居中显示
		frame.setLocationRelativeTo(null);
		//关闭窗口，把JVM停止
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//创建面板对象
		MyImage myImage = new MyImage();
		//把面板对象存放到窗口上
		frame.add(myImage);
		
		//调用初始化图片的方法
		myImage.initImgs();
		
		myImage.repaint();
		
		//显示出窗口
		frame.setVisible(true);
		
	}
	
		
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (bgImage != null) {
			g.drawImage(bgImage, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null);
		}
	}



	//定义图片类型的数组
	BufferedImage[] images = new BufferedImage[4];
	
	/**
	 * 加载提前准备好的一些图片
	 */
	public void initImgs() {
		try {
			for (int i = 0; i < 4; i++) {
					BufferedImage image = ImageIO.read(MyImage.class.getResource("/img/bg"+i+".png"));
					images[i-1] = image;
			}
			bgImage = images[0];
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
